package com.douglas.managesystem.controller.student;

import com.douglas.managesystem.common.ResponseResult;
import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.controller.BaseController;
import com.douglas.managesystem.entity.studentpojo.Teacher;
import com.douglas.managesystem.service.student.TeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 教师管理入口
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-17 12:41
 **/
@Controller
@RequestMapping("/stu")
public class TeacherController extends BaseController {



    @Autowired
    private TeacherService teacherService;


    /**
     * 1.分页教师列表
     * @param model
     * @param params
     * @return
     */
    @GetMapping("/teacher/list")
    public String list(Model model, SearchEntity params){
        PageInfo<Teacher> page = teacherService.findAllTeacherByPage(params);
        model.addAttribute("teacher",page);
        return "admin/teacher/list";
    }

    /**
     * 2.去新增页面
     * @return
     */
    @GetMapping("/teacher/toAdd")
    public String toAdd(){
        return "admin/teacher/add";
    }

    /**
     * 3.去编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/teacher/toEdit")
    public String toEdit(String id,Model model){
        Teacher teacher = teacherService.findTeacherById(id);
        model.addAttribute("teacher",teacher);
        return "admin/teacher/edit";
    }

    /**
     * 4.新增教师或编辑
     * @param teacher
     * @return
     */
    @PostMapping("/teacher/add")
    public String addOrEdit(Teacher teacher){
        if(StringUtils.isEmpty(teacher.getId())){
            //新增
            teacherService.addTeacher(teacher);
        }else{
            //编辑
            teacherService.editTeacher(teacher);
        }
        return "redirect:/stu/teacher/list";
    }


    /**
     * 5.根据id删除教师信息
     * @param id
     */
    @GetMapping("/teacher/delete")
    public String delete(String id){
        teacherService.deleteTeacherById(id);
        return "redirect:/stu/teacher/list";
    }

    /**
     * 6.验证教师工号是否存在，如存在提示无法添加
     * @param teacherId
     * @return
     */
    @GetMapping("/teacher/isTeacher")
    @ResponseBody
    public ResponseResult isTeacher(@RequestParam("teacherId") String teacherId){
        ResponseResult responseResult = new ResponseResult();
        Teacher teacher = teacherService.queryTeacherByTeacherId(teacherId);
        if(teacher != null){
            responseResult.setInfo("教师的工号已经存在，新增失败，请重新输入^_^");
            responseResult.setSuccess(false);
            return responseResult;
        }
        responseResult.setInfo("教师信息不存在，可以新增,Thanks");
        responseResult.setSuccess(true);
        return responseResult;
    }
}
